package service;

import mapper.BandwidthMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import po.Bandwidth;
import po.Result;

import java.util.List;

@Service
public class BandwidthServiceImpl implements BandwidthService {
    @Autowired
    private BandwidthMapper bandwidthMapper;

    @Override
    public List<Bandwidth> findAllBandwidths() throws Exception {
        return bandwidthMapper.findAllBandwidths();
    }

    @Override
    public Result addBandwidth(Bandwidth bandwidth) {
        Result result = new Result();
        try {
            bandwidthMapper.addBandwidth(bandwidth);
        } catch (Exception e) {
            result.setResult("fail");
            result.setMessage("添加失败！");
            return result;
        }
        result.setResult("success");
        result.setMessage("添加成功！");
        return result;
    }

    @Override
    public Result deleteBandwidthById(int id) {
        Result result = new Result();
        try {
            bandwidthMapper.deleteBandwidthById(id);
        } catch (Exception e) {
            result.setResult("fail");
            result.setMessage("删除失败！");
            return result;
        }
        result.setResult("success");
        result.setMessage("删除成功！");
        return result;
    }

    @Override
    public Result updateBandwidthById(int id, Bandwidth bandwidth) {
        Result result = new Result();
        try {
            bandwidth.setId(id);
            bandwidthMapper.updateBandwidthById(bandwidth);
        } catch (Exception e) {
            result.setResult("fail");
            result.setMessage("修改失败！");
            return result;
        }
        result.setResult("success");
        result.setMessage("修改成功！");
        return result;
    }
}
